﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security.Claims;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Identitycore.Controllers
{
    public class LoginController : Controller
    {
        public IActionResult Index()
        {
            /*
             登陆以后获取token
             获取传递的token 去找用户信息
             */

            string token = "1234565";
            string name = "wtmw";

            ClaimsIdentity identity = new ClaimsIdentity("Forms");
            identity.AddClaim(new Claim(ClaimTypes.Sid, token));
            identity.AddClaim(new Claim(ClaimTypes.Name, name));
            ClaimsPrincipal claimsPrincipal = new ClaimsPrincipal(identity);

            ///通过请求上下文保存 这个cookies
            ///
            HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, claimsPrincipal);

            return View();
        }
        /// <summary>
        ///   在当前授权中找到 AuthenticationSchemes 等于
        ///  cookie中这个名称  CookieAuthenticationDefaults.AuthenticationScheme
        /// </summary>
        /// <returns></returns>
        [Authorize(AuthenticationSchemes = CookieAuthenticationDefaults.AuthenticationScheme)]
        public IActionResult ShowTxt() {
            ///找到这个token
            string token = User.FindFirstValue(ClaimTypes.Sid);
            return View();
        }
    }
}